#
# $Id$
#
# RTEMS Project (http://www.rtems.org/)
# Copyright 2007 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of RTEMS Toolkit.
#
# RTEMS Toolkit is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the RTEMS Toolkit; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# Toolkit Grammer for Configuration files.
#

parser BuilderConfiguration:
    ignore:       "[ \t\r\n]+"
    ignore:       "#.*?\r?\n"
    token END:    "$"  # $ means end of string
    token STR:    '[rR]?\'([^\\n\'\\\\]|\\\\.)*\'|[rR]?"([^\\n"\\\\]|\\\\.)*"'
    token ID:     "[a-zA-Z_][a-zA-Z_0-9\\-\\.\\+]*"
    token MACRO:  "\\${[a-zA-Z_][a-zA-Z_0-9\\-\\.]*}"
    token MREF:   "\\${[a-zA-Z_][a-zA-Z_0-9\\-\\.]*}"
    token LIST:   ","
    token SEP:    ":"
    token FSLASH: "/"
    token BSLASH: "\\\\"
    token LEQ:    "<="
    token REQ:    "=>"
    token PIPE:   "\|"
    token IOIN:   ">"
    token NUM:    "[0-9]+"
    token VER:    "[0-9]([\\.\\-][0-9][\\.\\-0-9])+"
    token TEXT:   "([/a-zA-z_0-9\\-\\.\\*\\?\\${}:])+"

    rule Config: {{ groups = [] }}
                 (
                  Group
                  {{ groups.append(Group) }}
                 )*
                 {{ return('config', groups) }}
                 END
                 
    rule Group : Label
                 ( "{"
                     {{ members = [] }}
                     ( Member {{ members.append(Member) }} )+
                   "}"
                   {{ return('group', Label, members) }}
                  | "=" Element {{ return('macro', Label, Element) }} )
                
    rule Member: Label ":"
                  {{ members = [] }}
                  ( Element {{ members.append(Element) }} )+ ";"
                  {{ return ('member', Label, members) }}

    rule Label:   ID {{ return('id', ID, self._scanner.pos) }}
                | MACRO {{ return('mref', MACRO, self._scanner.pos) }}
                
    rule Macro: MACRO "=" Element {{ return('macro', MACRO, Element) }}

    rule Element:  ID     {{ return('id',      ID,        self._scanner.pos) }}
                 | LIST   {{ return('list',    ',',       self._scanner.pos) }}
                 | SEP    {{ return('sep',     ':',       self._scanner.pos) }}
                 | FSLASH {{ return('fslash',  '/',       self._scanner.pos) }}
                 | BSLASH {{ return('bslash',  '\\',      self._scanner.pos) }}
                 | LEQ    {{ return('leq',     '<=',      self._scanner.pos) }}
                 | REQ    {{ return('req',     '=>',      self._scanner.pos) }}
                 | PIPE   {{ return('pipe',    '|',       self._scanner.pos) }}
                 | IOIN   {{ return('redirin', '>',       self._scanner.pos) }}
                 | STR    {{ return('str',     eval(STR), self._scanner.pos) }}
                 | NUM    {{ return('num',     atoi(NUM), self._scanner.pos) }}
                 | VER    {{ return('ver',     VER,       self._scanner.pos) }}
                 | TEXT   {{ return('text',    TEXT,      self._scanner.pos) }}
